package com.raverun.imovies.jadeditor;

import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.application.ActionBarAdvisor;
import org.eclipse.ui.application.IActionBarConfigurer;
import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
import org.eclipse.ui.application.WorkbenchWindowAdvisor;
import org.eclipse.ui.IPartListener2;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPartReference;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.IPartService;

public class ApplicationWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor
{

    public ApplicationWorkbenchWindowAdvisor( IWorkbenchWindowConfigurer configurer )
    {
        super( configurer );
    }

    public ActionBarAdvisor createActionBarAdvisor( IActionBarConfigurer configurer )
    {
        return new ApplicationActionBarAdvisor( configurer );
    }

    public void preWindowOpen()
    {
        IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
        configurer.setInitialSize( new Point( 400, 300 ) );
        configurer.setShowCoolBar( false );
        configurer.setShowStatusLine( false );
        configurer.setTitle( "imovies JAD file editor" );
    }

    @Override
    public void postWindowOpen()
    {              
        IWorkbenchWindow wbWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if ( wbWindow != null )
        {
            listener = new IPartListener2() {

                public void partActivated( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partActivated" );
                    activateView();
                }

                // TODO move into a Command framework
                private void activateView()
                {
                    IWorkbenchWindow wbWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                    if ( wbWindow != null )
                    {
                        try
                        {
                            wbWindow.getActivePage().showView( View.ID );
                        }
                        catch( PartInitException pie )
                        {
                            // noop
                        }
                    }
                }

                public void partBroughtToTop( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partBroughtToTop" );
                }

                public void partClosed( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partClosed" );
                    
                    IWorkbenchWindow wbWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
                    if ( wbWindow != null )
                    {
                        wbWindow.getActivePage().hideView( wbWindow.getActivePage().findViewReference( View.ID ) ); 
                    }                    
                }

                public void partDeactivated( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partDeactivated" );
                }

                public void partHidden( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partHidden" );
                }

                public void partInputChanged( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partInputChanged" );
                }

                public void partOpened( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partOpened" );
                }

                public void partVisible( IWorkbenchPartReference partRef )
                {
                    System.out.println( "partVisible" );
                }};            
            
            
            IWorkbenchPage page = wbWindow.getActivePage();
            
            page.addPartListener( listener );
        }
    }
    
    @Override
    public void dispose()
    {
        IWorkbenchWindow wbWindow = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
        if ( wbWindow != null )
        {
            IWorkbenchPage page = wbWindow.getActivePage();
            page.removePartListener( listener );
        }
    }
    
    IPartListener2 listener;
}
